package com.my_musc_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my_musc_service.dao.SongDao;
import com.my_musc_service.dao.SongListUserDao;
import com.my_musc_service.entity.SharedSongListVO;
import com.my_musc_service.entity.Song;
import com.my_musc_service.entity.SongListUser;
import com.my_musc_service.entity.SonglistVO;
import com.my_musc_service.utils.StringUtils;
import com.my_musc_service.utils.spring.SpringUtils;
import lombok.NonNull;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author Zhu Dunfeng
 * @date 2021/8/28
 */
public interface SongListCommonProp {

    SongListUserDao songListUserDao = SpringUtils.getBean(SongListUserDao.class);
    SongDao songDao = SpringUtils.getBean(SongDao.class);

    public default <T> void setSongListCover(@NonNull List<T> songlistVOList) {
        //每一个分享歌单封面是其第一首歌的封面
        for (T songlistVO : songlistVOList) {
            if (songlistVO instanceof SonglistVO) {
                SonglistVO songlistVO1 = (SonglistVO) songlistVO;
                setVal(songlistVO1);
            } else if (songlistVO instanceof SharedSongListVO) {
                SharedSongListVO songlistVO1 = (SharedSongListVO) songlistVO;
                setSharedVal(songlistVO1);
            }


        }

    }

    default void setSharedVal(@NonNull SharedSongListVO sharedSongListVO) {
        Integer songListId = sharedSongListVO.getSongListId();
        Integer userId = sharedSongListVO.getUserId();
        setSongListVOCover(sharedSongListVO, songListId, userId);
    }


    default void setVal(@NonNull SonglistVO songlistVO) {
        Integer songListId = songlistVO.getId();
        Integer userId = songlistVO.getUserId();
        setSongListVOCover(songlistVO, songListId, userId);
    }

    default <T> void setSongListVOCover(T songListVO, Integer songListId, Integer userId) {
//        SongListUserDao songListUserDao = SpringUtils.getBean(SongListUserDao.class);
//        SongDao songDao = SpringUtils.getBean(SongDao.class);
        QueryWrapper<SongListUser> songListUserQueryWrapper = new QueryWrapper<>();
        songListUserQueryWrapper.eq("song_list_id", songListId);
        songListUserQueryWrapper.eq("user_id", userId);
        songListUserQueryWrapper.orderByAsc("song_index");
        List<SongListUser> songListUserList = songListUserDao.selectList(songListUserQueryWrapper);
        if (!CollectionUtils.isEmpty(songListUserList)) {
            SongListUser songListUser = songListUserList.get(0);
            Integer songId = songListUser.getSongId();
            Song song = songDao.selectOne(new QueryWrapper<Song>().eq("id", songId));
            if (song != null && StringUtils.isNotBlank(song.getCoverUrl())) {
                if (songListVO instanceof SharedSongListVO) {
                    ((SharedSongListVO) songListVO).setCoverUrl(song.getCoverUrl());
                } else if (songListVO instanceof SonglistVO) {
                    ((SonglistVO) songListVO).setCoverUrl(song.getCoverUrl());
                }
            }
        }
    }


}
